<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { watch, inject, reactive, Ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { Row } from '@/api/third'
const { required } = useValidator()
const emit = defineEmits(['close', 'get-list'])
const currentRow: Ref<Row> = inject('currentRow') as Ref<Row>
const { formRegister, formMethods } = useForm()

const { setValues } = formMethods

const schema = reactive<FormSchema[]>([
  {
    field: '',
    label: '基础信息',
    component: 'Divider',
    colProps: { span: 28 },
    componentProps: {
      borderStyle: 'none',
      contentPosition: 'left'
    }
  },
  {
    field: 'name',
    label: '司机名称',
    component: 'Input',
    formItemProps: {
      rules: [required(), { min: 1, max: 50, message: '最多输入50个字符', trigger: 'blur' }]
    },
    componentProps: {
      disabled: true,
      placeholder: '请输入(最多50个字符)'
    }
  },
  {
    field: 'mobile',
    label: '联系方式',
    component: 'Input',
    formItemProps: {
      rules: [
        required(),
        { min: 11, max: 11, message: '请输入联系方式', trigger: 'blur' },
        {
          pattern: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,
          message: '请输入负责人联系电话',
          trigger: 'blur'
        }
      ]
    },
    componentProps: {
      disabled: true,
      placeholder: '请输入联系方式'
    }
  },
  {
    field: 'sex',
    label: '性别',
    component: 'Select',
    formItemProps: {
      rules: [required()]
    },
    componentProps: {
      disabled: true,
      placeholder: '请选择',
      options: [
        {
          label: '男',
          value: 1
        },
        {
          label: '女',
          value: 2
        }
      ]
    }
  },
  {
    field: 'remark',
    label: '备注',
    component: 'Input',
    componentProps: {
      disabled: true,
      placeholder: '请输入'
    }
  },
  {
    field: '',
    label: '财务信息',
    component: 'Divider',
    colProps: { span: 28 },
    componentProps: {
      borderStyle: 'none',
      contentPosition: 'left'
    }
  },
  {
    field: 'bank_account_name',
    label: '户名',
    component: 'Input',
    componentProps: {
      disabled: true,
      placeholder: '请输入'
    }
  },
  {
    field: 'bank_type',
    label: '所属银行',
    component: 'Select',
    componentProps: {
      disabled: true,
      placeholder: '请选择',
      options: [
        {
          label: '中国银行',
          value: 1
        },
        {
          label: '建设银行',
          value: 2
        },
        {
          label: '工商银行',
          value: 3
        },
        {
          label: '农业银行',
          value: 4
        },
        {
          label: '交通银行',
          value: 5
        },
        {
          label: '招商银行',
          value: 6
        },
        {
          label: '浦发银行',
          value: 7
        },
        {
          label: '兴业银行',
          value: 8
        },
        {
          label: '民生银行',
          value: 9
        },
        {
          label: '光大银行',
          value: 10
        },
        {
          label: '广发银行',
          value: 11
        },
        {
          label: '华夏银行',
          value: 12
        },
        {
          label: '平安银行',
          value: 13
        },
        {
          label: '邮政储蓄银行',
          value: 14
        },
        {
          label: '其他',
          value: 15
        }
      ]
    }
  },
  {
    field: 'bank_name',
    label: '开户行',
    component: 'Input',
    formItemProps: {
      rules: [{ min: 1, max: 100, message: '最多输入100个字符', trigger: 'blur' }]
    },
    componentProps: {
      disabled: true,
      placeholder: '请输入(最多100个字符)'
    }
  },
  {
    field: 'bank_no',
    label: '银行卡号',
    component: 'Input',
    formItemProps: {
      rules: [
        {
          pattern: /^[1-9]\d{9,29}$/,
          message: '请核对银行卡号',
          trigger: 'blur'
        }
      ]
    },
    componentProps: {
      disabled: true,
      placeholder: '请输入'
    }
  },
  {
    field: '',
    label: '证件信息',
    component: 'Divider',
    colProps: { span: 28 },
    componentProps: {
      borderStyle: 'none',
      contentPosition: 'left'
    }
  },
  {
    field: 'license_front',
    label: '司机驾驶证（正面）',
    formItemProps: {
      slots: {
        default: (data) => {
          if (data.license_front) {
            return (
              <img
                src={data.license_front}
                style={{
                  width: '150px',
                  height: '150px',
                  objectFit: 'cover'
                }}
              />
            )
          } else {
            return <div>没有图片</div>
          }
        }
      }
    }
  },
  {
    field: 'license_back',
    label: '司机驾驶证（反面）',
    formItemProps: {
      slots: {
        default: (data) => {
          if (data.license_back) {
            return (
              <img
                src={data.license_back}
                style={{
                  width: '150px',
                  height: '150px',
                  objectFit: 'cover'
                }}
              />
            )
          } else {
            return <div>没有图片</div>
          }
        }
      }
    }
  },
  {
    field: 'id_card_front',
    label: '司机身份证(正面)',
    formItemProps: {
      slots: {
        default: (data) => {
          if (data.id_card_front) {
            return (
              <img
                src={data.id_card_front}
                style={{
                  width: '150px',
                  height: '150px',
                  objectFit: 'cover'
                }}
              />
            )
          } else {
            return <div>没有图片</div>
          }
        }
      }
    }
  },
  {
    field: 'id_card_back',
    label: '司机身份证(反面)',
    formItemProps: {
      slots: {
        default: (data) => {
          if (data.id_card_back) {
            return (
              <img
                src={data.id_card_back}
                style={{
                  width: '150px',
                  height: '150px',
                  objectFit: 'cover'
                }}
              />
            )
          } else {
            return <div>没有图片</div>
          }
        }
      }
    }
  },
  {
    field: 'license',
    label: '驾驶证号',
    component: 'Input',
    formItemProps: {
      rules: [
        {
          pattern: /^[0-9]{12}$/,
          message: '请核对银行卡号',
          trigger: 'blur'
        }
      ]
    },
    componentProps: {
      disabled: true,
      placeholder: '请输入'
    }
  },
  {
    field: 'id_card',
    label: '身份证号码',
    component: 'Input',
    formItemProps: {
      rules: [
        {
          pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
          message: '请核对银行卡号',
          trigger: 'blur'
        }
      ]
    },
    componentProps: {
      disabled: true,
      placeholder: '请输入'
    }
  },
  {
    field: '',
    label: '账号信息',
    component: 'Divider',
    colProps: { span: 28 },
    componentProps: {
      borderStyle: 'none',
      contentPosition: 'left'
    }
  },
  {
    field: 'user_name',
    label: '登录账号',
    component: 'Input',
    formItemProps: {
      rules: [{ min: 1, max: 20, message: '最多输入20个字符', trigger: 'blur' }]
    },
    componentProps: {
      disabled: true,
      placeholder: '请输入(最多20个字符)'
    }
  },
  {
    field: 'password',
    label: '登录密码',
    component: 'Input',
    formItemProps: {
      rules: [{ min: 6, max: 20, message: '输入6-20个字符', trigger: 'blur' }]
    },
    componentProps: {
      disabled: true,
      placeholder: '输入6-20个字符'
    }
  }
])

watch(
  () => currentRow.value,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)
</script>

<template>
  <ContentDetailWrap title="司机详情">
    <Form @register="formRegister" :schema="schema" />
    <template #footer>
      <div class="w-full flex items-center justify-center">
        <BaseButton @click="emit('close')">返回</BaseButton>
      </div>
    </template>
  </ContentDetailWrap>
</template>
